<?php

class LabelsController extends Controller
{
	public function actionView($slug)
	{
		$model = Labels::model()->findByAttributes(array('slug'=>$slug));
		
        // Events
		$criteria = new CDbCriteria;
		$criteria->condition = 't.status="A"';
		$criteria->with = array('eventLabel'=>array(
			'joinType'=>'INNER JOIN',
			'condition'=>'eventLabel.label_id='.$model->id,
            'together'=>true
		));
        
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate DESC, enddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		// News
        $criteria = new CDbCriteria;
		$criteria->condition = 't.status="A"';
        $criteria->limit = 10;
		$criteria->with = array('newsLabel'=>array(
			'joinType'=>'INNER JOIN',
			'condition'=>'newsLabel.label_id='.$model->id,
            'together'=>true
		));
        
        $news = News::model()->findAll($criteria);
        
		$this->render('view', array(
			'model'=>$model,
			'dataProvider'=>$dataProvider,
            'news'=>$news
		));
	}
	
	/* Label with News & Events
	$sql = 'SELECT * FROM ( SELECT e.id id, title, image, e.url slug, posteddate, l.type, e.startdate brief FROM events e INNER JOIN event_labels l ON e.id = l.event_id WHERE l.label_id = ' . $model->id . ' UNION SELECT n.id id, title, image, slug, posteddate, l.type, n.description brief FROM news n INNER JOIN event_labels l ON n.id = l.event_id WHERE l.label_id = ' . $model->id . ') as x ORDER BY posteddate DESC';
		
	$dataProvider=new CSqlDataProvider($sql, array(
		'keyField'=>'id',
		'totalItemCount'=>count(Yii::app()->db->createCommand($sql)->queryAll()),
		'pagination'=>array(
			'pageSize'=>10,
		),
	));
	$this->render('view', array(
		'model'=>$model,
		'dataProvider'=>$dataProvider,
	));
	
	*/
}